package com.leha.core.services.imp;

import com.github.pagehelper.PageHelper;
import com.leha.core.dao.*;
import com.leha.core.entity.mybatis.*;
import com.leha.core.entity.vo.ImgAndQuestions_;
import com.leha.core.entity.vo.ImgsAndQuestionsVO;
import com.leha.core.services.GameZZLDSServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

@Service
public class GameZZLDSServicesImpl implements GameZZLDSServices {
    @Autowired
    LhGameJYLCustomMapper lhGameJYLCustomMapper;
    @Autowired
    LhGjylQuestionMapper lhGjylQuestionMapper;
    @Autowired
    LhGjylImgsMapper imgsMapper;
    @Autowired
    LhRewardMapper rewardMapper;

    @Override
    public List<ImgsAndQuestionsVO> getQuestions(Integer uid) {
        List<ImgAndQuestions_> ret = lhGameJYLCustomMapper.randImgsForUser(uid, 10);
        ret.sort(Comparator.naturalOrder());
        List<ImgsAndQuestionsVO> rr = new ArrayList<>();
        Random rand=new Random();
        int len = ret.size();
        int lv1 = len/3;
        int lv2 = 2*len/3;
        for(int i=0;i<len;i++) {
            List<LhGjylQuestion> questions = ret.get(i).getQuestions();
            questions.sort(Comparator.naturalOrder());
            int qlen = questions.size();
            int qstep = qlen / 3;
            int j = 0;
            if(i < lv1) { //简单题
                j = rand.nextInt(qstep);
            } else if(i<lv2) {//中等题
                j = qstep + rand.nextInt(qstep);
            } else { //难题从难度高的往下随机找
                j = qlen - 1 - rand.nextInt(qstep);
            }
            ImgsAndQuestionsVO t = new ImgsAndQuestionsVO(questions.get(j).getQuestionTitle(),
                    questions.get(j).getQuestionId(),
                    questions.get(j).getQuestionSection(),
                    ret.get(i).getImgs().getImgUrl());
            rr.add(t);
        }
        return rr;
    }

    @Override
    public Boolean checkQuestionAnswer(Integer qid, Integer choose) {
        if(choose==null || choose<1 || choose > 4) return false;
        LhGjylQuestion question = lhGjylQuestionMapper.selectByPrimaryKey(qid);
        lhGameJYLCustomMapper.updateQuestionCnt(qid);
        if(question != null && question.getQuestionAnswer() == choose.byteValue()) {
            return true;
        }
        return false;
    }

    @Override
    public List<LhGameLog> getCanExchangeGameLog(Integer uid) {
        return lhGameJYLCustomMapper.getActiveGameRec(uid);
    }

    @Override
    public List<LhGjylImgs> getImages4Admin(Integer pgNum, Integer pgSize) {
        PageHelper.startPage(pgNum, pgSize);
        return imgsMapper.selectByExample(new LhGjylImgsExample());
    }

    @Override
    public List<LhGjylQuestion> getQuestions4Admin(Integer pgNum, Integer pgSize, Integer imgId, String query) {
        LhGjylQuestionExample ex = new LhGjylQuestionExample();
        if(imgId!=null && imgId!=0) {
            if(query !=null && !"".equals(query)) {
                ex.createCriteria().andImgIdEqualTo(imgId).andQuestionTitleLike("%"+query+"%");
            } else {
                ex.createCriteria().andImgIdEqualTo(imgId);
            }
        } else {
            if(query != null && !"".equals(query)){
                ex.createCriteria().andQuestionTitleLike("%"+query+"%");
            }
        }

        PageHelper.startPage(pgNum, pgSize);
        ex.setOrderByClause("img_id desc");
        return  lhGjylQuestionMapper.selectByExample(ex);
    }

    @Override
    public LhGjylImgs SaveImgs(Integer imgId, String url) {
        LhGjylImgs img = new LhGjylImgs();
        if(imgId==null || imgId==0) {
            img.setCount(0);
            img.setImgUrl(url);
            img.setStatus(0);
            imgsMapper.insertSelective(img);
        } else {
            img.setImgId(imgId);
            img.setImgUrl(url);
            imgsMapper.updateByPrimaryKeySelective(img);
        }
        return img;
    }

    @Override
    public Boolean SaveQuestion(LhGjylQuestion question) {
        if(question.getQuestionId()==null || question.getQuestionId()==0) {
            return lhGjylQuestionMapper.insertSelective(question)>0;
        } else {
            return lhGjylQuestionMapper.updateByPrimaryKeySelective(question)>0;
        }
    }

    @Override
    public LhGjylQuestion getQuestionById4Admin(Integer questionId) {
        return lhGjylQuestionMapper.selectByPrimaryKey(questionId);
    }

    @Override
    public List<LhReward> getExchanges4Admin(Integer pgNum, Integer pgSize, Integer status) {
        PageHelper.startPage(pgNum, pgSize);
        LhRewardExample ex = new LhRewardExample();
        if(status!=null) {
            ex.createCriteria().andStatsEqualTo(status);
        }
        ex.setOrderByClause("insert_time desc");
        return rewardMapper.selectByExample(ex);
    }

    @Override
    public Integer SaveRewardStatus(Integer rewardId, Integer status) {
        LhReward re = rewardMapper.selectByPrimaryKey(rewardId);
        re.setStats(status);
        //re.setName("hh");
        int ret = rewardMapper.updateByPrimaryKey(re);
        return  ret;
    }
}
